﻿using Single.Core.Service;
using Single.Sugar.Entity;
using Single.Sugar.Service;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Single.Sys.Service
{
    public class SysOtherService : SugarService
    {
        public ServiceResult GetEntityCodeByTableName(string tableName)
        {
            var list = GetPropertiesByTableName(tableName);

            if (list.Count > 0)
            {
                var sb = new StringBuilder();

                sb.AppendLine("[Tenant(\"SingleDefault\")]");

                sb.AppendLine($"public class {GetEntityCodeName(tableName)} : BaseEntity");

                sb.AppendLine("{");

                foreach (var col in list)
                {
                    sb.AppendLine($"    public {col.Value} {col.Key} {{ get; set; }}");
                }

                sb.AppendLine("}");

                return SuccessResult(data: sb.ToString());
            }

            return FailureResult();
        }

        public ServiceResult GetColumnsDataByTableName(string tableName)
        {
            return SuccessResult(data: GetPropertiesByTableName(tableName));
        }

        private Dictionary<string, string> GetPropertiesByTableName(string tableName)
        {
            string sql = "SELECT COLUMN_NAME,DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=@tableName";
            var list = db.Ado.SqlQuery<dynamic>(sql, new { tableName }).ToList();

            var baseType = typeof(BaseEntity);
            var dic = new Dictionary<string, string>();

            foreach (var col in list)
            {
                var colName = GetEntityCodeName(col.COLUMN_NAME as string);
                if (baseType.GetProperty(colName) != null) { continue; }

                var entityType = "string";
                var type = col.DATA_TYPE as string;

                switch (type)
                {
                    case "bigint":
                        entityType = "long"; break;
                    case "int":
                        entityType = "int"; break;
                    case "datetime":
                        entityType = "DateTime"; break;
                    case "decimal":
                        entityType = "decimal"; break;
                    case "bit":
                        entityType = "bool"; break;
                    default: break;
                }

                dic.Add(colName, entityType);
            }

            return dic;
        }

        private string GetEntityCodeName(string? lineName)
        {
            if (!string.IsNullOrEmpty(lineName))
            {
                var newName = string.Empty;

                var arr = lineName.Split("_");

                foreach (var ar in arr)
                {
                    newName += Char.ToUpper(ar[0]) + ar.Substring(1);
                }

                return newName;
            }

            return string.Empty;
        }
    }
}